package cn.com.shadowless.xinning.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;

import com.mengpeng.mphelper.ToastUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.shadowless.xinning.MyApplication;
import cn.com.shadowless.xinning.R;
import cn.com.shadowless.xinning.utils.DialogUtils;
import cn.com.shadowless.xinning.utils.FragmentUtils;
import cn.com.shadowless.xinning.utils.Item;
import cn.com.shadowless.xinning.utils.PreferencesUtils;
import cn.com.shadowless.xinning.view.BaseActivity;
import cn.com.shadowless.xinning.view.fragment.AdminiStrativeFragment;
import cn.com.shadowless.xinning.view.fragment.ApprovalFragment;
import cn.com.shadowless.xinning.view.fragment.AssetApplyFragment;
import cn.com.shadowless.xinning.view.fragment.AssetBrokenFragment;
import cn.com.shadowless.xinning.view.fragment.AssetMoveFragment;
import cn.com.shadowless.xinning.view.fragment.AssetPayFragment;
import cn.com.shadowless.xinning.view.fragment.AssetRegisterCardFragment;
import cn.com.shadowless.xinning.view.fragment.CarFragment;
import cn.com.shadowless.xinning.view.fragment.CommunicateFragment;
import cn.com.shadowless.xinning.view.fragment.CompanyBudgetFragment;
import cn.com.shadowless.xinning.view.fragment.ContractReviewFragment;
import cn.com.shadowless.xinning.view.fragment.CostFragment;
import cn.com.shadowless.xinning.view.fragment.DepartBudgetFragment;
import cn.com.shadowless.xinning.view.fragment.DepartureFragment;
import cn.com.shadowless.xinning.view.fragment.GeneralFragment;
import cn.com.shadowless.xinning.view.fragment.MaintenanceFragment;
import cn.com.shadowless.xinning.view.fragment.ProcureFragment;
import cn.com.shadowless.xinning.view.fragment.PublicAssetsApplyFragment;
import cn.com.shadowless.xinning.view.fragment.SupplierFragment;
import cn.com.shadowless.xinning.view.fragment.TrainFragment;
import cn.com.shadowless.xinning.view.fragment.TransportationFragment;
import cn.com.shadowless.xinning.view.fragment.TravelFragment;

/**
 * The type Order more info activity.
 *
 * @author sHadowLess
 */
public class OrderMoreInfoActivity extends BaseActivity {

    /**
     * The Order more back btn.
     */
    @BindView(R.id.orderMore_back_btn)
    ImageView orderMoreBackBtn;
    /**
     * The Order more title.
     */
    @BindView(R.id.orderMore_title)
    TextView orderMoreTitle;
    /**
     * The Order more fragment.
     */
    @BindView(R.id.orderMore_fragment)
    FrameLayout orderMoreFragment;
    /**
     * The Order more checkout btn.
     */
    @BindView(R.id.orderMore_checkout_Btn)
    Button orderMoreCheckoutBtn;
    /**
     * The Order more back to btn.
     */
    @BindView(R.id.orderMore_backTo_Btn)
    Button orderMoreBackToBtn;
    /**
     * The Order more all back btn.
     */
    @BindView(R.id.orderMore_allBack_Btn)
    Button orderMoreAllBackBtn;
    /**
     * The Check btns.
     */
    @BindView(R.id.check_btns)
    LinearLayout checkBtns;
    /**
     * The Order more check car btn.
     */
    @BindView(R.id.orderMore_checkCar_Btn)
    Button orderMoreCheckCarBtn;
    /**
     * The Order more back car btn.
     */
    @BindView(R.id.orderMore_backCar_Btn)
    Button orderMoreBackCarBtn;
    /**
     * The Car btns.
     */
    @BindView(R.id.car_btns)
    LinearLayout carBtns;

    private List<Item> list = new ArrayList<>();
    private String billCode;
    private String userName;
    private String orderId;
    private Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    list.clear();
                    list.addAll((List<Item>) msg.obj);
                    orderMoreCheckCarBtn.setEnabled(true);
                    break;
                case 2:
                    checkBtns.setVisibility(View.GONE);
                    carBtns.setVisibility(View.VISIBLE);
                    break;
                default:
                    break;
            }
        }
    };

    @Override
    protected int setLayout() {
        return R.layout.activity_order_more_info;
    }

    @Override
    protected void setData() {
        Intent intent = getIntent();
        String billName = intent.getStringExtra("billName");
        orderId = intent.getStringExtra("orderId");
        billCode = intent.getStringExtra("billCode");
        userName = intent.getStringExtra("userName");
        if (null != orderId && null != billCode) {
            orderMoreTitle.setText(billName);
            Bundle bundle = new Bundle();
            bundle.putString("orderId", orderId);
            bundle.putString("userName", userName);
            bundle.putString("billCode", billCode);
            switch (billCode) {
                //公司年度
                case "BC":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    CompanyBudgetFragment companyBudgetFragment = new CompanyBudgetFragment();
                    companyBudgetFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, companyBudgetFragment);
                    break;
                //部门年度
                case "BB":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    DepartBudgetFragment departBudgetFragment = new DepartBudgetFragment();
                    departBudgetFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, departBudgetFragment);
                    break;
                //差旅申请
                case "TR":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    TravelFragment travelFragment = new TravelFragment();
                    travelFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, travelFragment);
                    break;
                //采购申请
                case "PO":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    ProcureFragment procureFragment = new ProcureFragment();
                    procureFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, procureFragment);
                    break;
                //运输结算
                case "PY":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    TransportationFragment transportationFragment = new TransportationFragment();
                    transportationFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, transportationFragment);
                    break;
                //费用申请
                case "FY":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    CostFragment costFragment = new CostFragment();
                    costFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, costFragment);
                    break;
                //离职申请
                case "LZ":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    DepartureFragment departureFragment = new DepartureFragment();
                    departureFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, departureFragment);
                    break;
                //培训申请
                case "PX":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    TrainFragment trainFragment = new TrainFragment();
                    trainFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, trainFragment);
                    break;
                //交际申请
                case "JF":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    CommunicateFragment communicateFragment = new CommunicateFragment();
                    communicateFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, communicateFragment);
                    break;
                //通用申请
                case "TB":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    GeneralFragment generalFragment = new GeneralFragment();
                    generalFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, generalFragment);
                    break;
                //服务、维修、保养申请
                case "WX":
                    break;
                //服务、维修、保养确认
                case "WR":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    MaintenanceFragment maintenanceFragment = new MaintenanceFragment();
                    maintenanceFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, maintenanceFragment);
                    break;
                //车辆使用
                case "YC":
                    CarFragment carFragment = new CarFragment(handler);
                    carFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, carFragment);
                    break;
                //行政事务
                case "XB":
                    checkBtns.setVisibility(View.VISIBLE);
                    carBtns.setVisibility(View.GONE);
                    AdminiStrativeFragment adminiStrativeFragment = new AdminiStrativeFragment();
                    adminiStrativeFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, adminiStrativeFragment);
                    break;
                //固定资产登记卡
                case "DJ":
                    AssetRegisterCardFragment assetRegisterCardFragment = new AssetRegisterCardFragment();
                    assetRegisterCardFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, assetRegisterCardFragment);
                    break;
                //固定资产调拨
                case "DB":
                    AssetMoveFragment assetMoveFragment = new AssetMoveFragment();
                    assetMoveFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, assetMoveFragment);
                    break;
                //固定资产报废
                case "BF":
                    AssetBrokenFragment assetBrokenFragment = new AssetBrokenFragment();
                    assetBrokenFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, assetBrokenFragment);
                    break;
                //供应商
                case "SP":
                    SupplierFragment supplierFragment = new SupplierFragment();
                    supplierFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, supplierFragment);
                    break;
                //公共资产
                case "PR":
                    PublicAssetsApplyFragment publicAssetsApplyFragment = new PublicAssetsApplyFragment();
                    publicAssetsApplyFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, publicAssetsApplyFragment);
                    break;
                //合同
                case "CP":
                    ContractReviewFragment contractReviewFragment = new ContractReviewFragment();
                    contractReviewFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, contractReviewFragment);
                    break;
                //送审签
                case "AP":
                    ApprovalFragment approvalFragment = new ApprovalFragment();
                    approvalFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, approvalFragment);
                    break;
                //固定资产采购申请
                case "FA":
                    AssetApplyFragment assetApplyFragment = new AssetApplyFragment();
                    assetApplyFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, assetApplyFragment);
                    break;
                //固定资产报销
                case "FB":
                    AssetPayFragment assetPayFragment = new AssetPayFragment();
                    assetPayFragment.setArguments(bundle);
                    FragmentUtils.replaceFragment(fragmentManager, R.id.orderMore_fragment, assetPayFragment);
                    break;
                default:
                    break;
            }
        } else {
            ToastUtils.onErrorShowToast("此单无编号，请联系管理员");
        }
    }

    /**
     * On view clicked.
     *
     * @param view the view
     */
    @OnClick({R.id.orderMore_checkout_Btn, R.id.orderMore_backTo_Btn, R.id.orderMore_allBack_Btn, R.id.orderMore_checkCar_Btn, R.id.orderMore_backCar_Btn, R.id.orderMore_back_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.orderMore_checkout_Btn:
                DialogUtils.showCheckMessageDialog(this, "审核通过", billCode, userName, orderId);
                break;
            case R.id.orderMore_backTo_Btn:
                DialogUtils.showCheckMessageDialog(this, "逐级驳回", billCode, userName, orderId);
                break;
            case R.id.orderMore_allBack_Btn:
            case R.id.orderMore_backCar_Btn:
                DialogUtils.showCheckMessageDialog(this, "彻底驳回", billCode, userName, orderId);
                break;
            case R.id.orderMore_checkCar_Btn:
                DialogUtils.showCarNoDialog(this, list, billCode, userName, orderId);
                break;
            case R.id.orderMore_back_btn:
                startActivity(new Intent(this, OrderActivity.class));
                finish();
                break;
            default:
                break;
        }
    }

    @Override
    public void onBackPressed() {
        startActivity(new Intent(this, OrderActivity.class));
        finish();
    }
}